﻿// // This code is released under the BSD license.
namespace PugLib
{
	using System;

	/// <summary>
	/// http://rogeralsing.com/2008/04/17/disposer-idisposable-and-template-pattern/
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public sealed class Disposer<T> : IDisposable
		where T : IDisposable
	{
		private readonly bool _shouldDispose;
		private readonly T _value;

		public Disposer(T value, bool shouldDispose)
		{
			_value = value;
			_shouldDispose = shouldDispose;
		}

		public T Value
		{
			get { return _value; }
		}

		public bool ShouldDispose
		{
			get { return _shouldDispose; }
		}

		#region IDisposable Members

		public void Dispose()
		{
			if (!_shouldDispose)
			{
				return;
			}

			if (_value != null)
			{
				_value.Dispose();
			}
			GC.SuppressFinalize(this);
		}

		#endregion
	}
}